define(['angularAMD','module/moduleB/program/B02/B0217/BadReasonService'], function (angularAMD) {
    angularAMD.service('panelInputService', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog','$calculater', 'BadReasonService', 
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $calculater, $BadReasonService){
            var _service = this;        
            _service.init = function(options){
                $mdDialog.dialog(
                    'module/moduleB/program/B02/B0217/panelInput.html',
                    function(dialog){
                        return {
                            css_url:'module/moduleB/program/B02/B0217/panelInput.css',
                            itemList: options.itemList,
                            CheckQC: config.cache.sys_parameter['CheckQC'],
                            osbacknotallowedexceed:config.cache.sys_parameter['OSBackNotAllowedExceed'],
                            osvaluationinput:config.cache.sys_parameter['OSValuationInput'],
                            beforeShown: function(){
                                dialog.Qty_Update();
                            },
                            back: function () {
                                dialog.hide();
                                if(options.close)
                                    options.close();
                            },
                            confirm: function () {
                                var msg = '';
                                msg = dialog.Qty_check(msg);
                                if(msg!='')
                                {
                                    $rootScope.showAlert(msg);
                                }else{
                                    if(options.callback)
                                    options.callback(dialog);
                                }       
                            },

                            // 更改良品數
                            returned_qty : function (event, item) {
                                var target = event.currentTarget;
                                angular.element(target).addClass('is-focus');
                                $calculater.open(function (num, calcDialog) {
                                    if( (+item.returned_qty) == (+num))
                                    {
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                        return;
                                    }else{
                                        if((+num) < 0){
                                            $rootScope.showAlert($filter('translate')('panelInput.msg.qty_error'));
                                            return;
                                        } 
                                        if(((+num)>(+dialog.itemList.can_returned_qty)) && (dialog.osbacknotallowedexceed =="1")){
                                            $rootScope.showAlert($filter('translate')('panelInput.msg.OSReceipt_Qtyover'));       //外包超收                                           
                                        }else{
                                            item.returned_qty = num;
                                            angular.element(target).removeClass('is-focus');
                                            dialog.alter_valuationqty(); 
                                            calcDialog.hide();
                                            dialog.Qty_Update();
                                        }
                                    }
                                },
                                    function (calcDialog) {
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                    },
                                    {
                                        defaultNum: item.returned_qty,
                                        decimalLength: 4 //20180531 modify by Dustdusk for M#47421:回貨小數位數開啟
                                    });
                            },

                            // 更改多餘數
                            excessqty: function(event, item){
                                var target = event.currentTarget;
                                angular.element(target).addClass('is-focus');
                                $calculater.open(function (num, calcDialog) {
                                    if( (+item.excessqty) == (+num))
                                    {
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                        return;
                                    }else{
                                        if((+num) < 0){
                                            $rootScope.showAlert($filter('translate')('panelInput.msg.qty_error'));
                                            return;
                                        } 
                                        item.excessqty = num;
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                        dialog.Qty_Update();
                                    }
                                },
                                    function (calcDialog) {
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                    },
                                    {
                                        defaultNum: item.excessqty,
                                        decimalLength: 4 
                                    });
                            },

                            //更改不良數
                            abnormal_qty: function(event, item){
                                $BadReasonService.init({
                                    parent: 'B0217',
                                    plot_no: item.plot_no,
                                    op_no: item.op_no,
                                    abnormal_detail: angular.copy(item.abnormal_detail),
                                    callback: function(dialog_badreason){
                                        if((+item.abnormal_qty) == (+dialog_badreason.total_abnormal_qty))
                                        {
                                            dialog_badreason.hide();
                                            return;
                                        }
                                        item.abnormal_detail = dialog_badreason.badReasonInfo;
                                        item.abnormal_qty = dialog_badreason.total_abnormal_qty; // 回貨不良數
                                        dialog_badreason.hide();
                                        dialog.Qty_Update();
                                    }
                                });                                          
                            },

                            //更改驗退數
                            return_qty: function(event, item){
                                var target = event.currentTarget;
                                angular.element(target).addClass('is-focus');
                                $calculater.open(function (num, calcDialog) {
                                    if( (+item.return_qty) == (+num))
                                    {
                                        angular.element(target).removeClass('is-focus');
                                        dialog.hide();
                                        return;
                                    }
                                    if((+num) < 0){
                                        $rootScope.showAlert($filter('translate')('panelInput.msg.qty_error'));
                                        return;
                                    } 
                                    item.return_qty = num;
                                    angular.element(target).removeClass('is-focus');
                                    calcDialog.hide();
                                    dialog.Qty_Update();
                                },
                                    function (calcDialog) {
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                    },
                                    {
                                        defaultNum: item.return_qty,
                                        decimal : false
                                    });
                            },

                            //更改短少數
                            lossqty: function(event, item){
                                var target = event.currentTarget;
                                angular.element(target).addClass('is-focus');
                                $calculater.open(function (num, calcDialog) {
                                    if( (+item.lossqty) == (+num))
                                    {
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                        return;
                                    }else{
                                        if((+num) < 0){
                                            $rootScope.showAlert($filter('translate')('panelInput.msg.qty_error'));
                                            return;
                                        }  
                                        item.lossqty = num;
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                        dialog.Qty_Update();
                                    }
                                },
                                    function (calcDialog) {
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                    },
                                    {
                                        defaultNum: item.lossqty,
                                        decimalLength: 4 
                                    });
                            },

                            //更改發料不良數
                            issuedefectqty: function(event, item){
                                var target = event.currentTarget;
                                angular.element(target).addClass('is-focus');
                                $calculater.open(function (num, calcDialog) {
                                    if( (+item.issuedefectqty) == (+num))
                                    {
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                        return;
                                    }else{
                                        if((+num) < 0){
                                            $rootScope.showAlert($filter('translate')('panelInput.msg.qty_error'));
                                            return;
                                        }  
                                        item.issuedefectqty = num;
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                        dialog.alter_valuationqty();                                    
                                        dialog.Qty_Update();
                                    }
                                },
                                    function (calcDialog) {
                                        angular.element(target).removeClass('is-focus');              
                                        calcDialog.hide();
                                    },
                                    {
                                        defaultNum: item.issuedefectqty,
                                        decimalLength: 4 
                                    });
                            },

                            //更改計價數量
                            valuationqty: function(event, item){
                                var target = event.currentTarget;
                                angular.element(target).addClass('is-focus');
                                $calculater.open(function (num, calcDialog) {
                                    if( (+item.valuationqty) == (+num))
                                    {
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                        return;
                                    }else{
                                        if((+num) < 0){
                                            $rootScope.showAlert($filter('translate')('panelInput.msg.qty_error'));
                                            return;
                                        }  
                                        if((+num) < (+item.issuedefectqty)){
                                            $rootScope.showAlert($filter('translate')('panelInput.msg.valuationqty_error'));
                                            return;
                                        }
                                        if((+num) > ((+item.returned_qty) + (+item.issuedefectqty))){
                                            $rootScope.showAlert($filter('translate')('panelInput.msg.valuationqty_over'));
                                            return;
                                        }
                                        item.valuationqty = num;
                                        item.valuationqty_no = (+item.returned_qty) + (+item.issuedefectqty) - (+item.valuationqty)
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                        dialog.Qty_Update();
                                    }
                                },
                                    function (calcDialog) {
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                    },
                                    {
                                        defaultNum: item.valuationqty,
                                        decimalLength: 4 
                                    });
                            },
                            
                            //更改不計價數量
                            valuationqty_no: function(event, item){
                                var target = event.currentTarget;
                                angular.element(target).addClass('is-focus');
                                $calculater.open(function (num, calcDialog) {
                                    if( (+item.valuationqty_no) == (+num))
                                    {
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                        return;
                                    }else{
                                        if((+num) < 0){
                                            $rootScope.showAlert($filter('translate')('panelInput.msg.qty_error'));
                                            return;
                                        } 
                                        if(((+item.returned_qty) - (+num)) < 0){
                                            $rootScope.showAlert($filter('translate')('panelInput.msg.valuationqty_error'));
                                            return;
                                        }
                                        if((+item.valuationqty)-(+num) > ((+item.returned_qty) + (+item.issuedefectqty))){
                                            $rootScope.showAlert($filter('translate')('panelInput.msg.valuationqty_over'));
                                            return;
                                        }
                                        item.valuationqty_no = num;
                                        item.valuationqty = (+item.returned_qty) + (+item.issuedefectqty) - (+item.valuationqty_no)                                    
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                        dialog.Qty_Update();
                                    }
                                },
                                    function (calcDialog) {
                                        angular.element(target).removeClass('is-focus');
                                        calcDialog.hide();
                                    },
                                    {
                                        defaultNum: item.valuationqty_no,
                                        decimalLength: 4 
                                    });
                            },

                            //更新總計欄位
                            Qty_Update: function(){
                                //20220221 modify by 10877 FOR MTS0107482  ========(S)========
                                // dialog.itemList.total = (+dialog.itemList.returned_qty) + (+dialog.itemList.abnormal_qty) + 
                                //     (+dialog.itemList.lossqty) + (+dialog.itemList.issuedefectqty);
                                dialog.itemList.total = (+dialog.itemList.returned_qty) + (+dialog.itemList.abnormal_qty) + 
                                    (+dialog.itemList.lossqty) + (+dialog.itemList.issuedefectqty) - (+dialog.itemList.excessqty);
                                //20220221 modify by 10877 FOR MTS0107482  ========(E)========
                                    
                                //20220221 modify by 10877 FOR MTS01074822  ========(S)========
                                // dialog.itemList.total_returned_qty = (+dialog.itemList.returned_qty) + (+dialog.itemList.excessqty);
                                dialog.itemList.total_returned_qty = (+dialog.itemList.returned_qty);
                                //20220221 modify by 10877 FOR MTS01074822  ========(E)========

                                dialog.itemList.total_bad = (+dialog.itemList.abnormal_qty) + (+dialog.itemList.return_qty)

                                dialog.itemList.total_valuationqty = dialog.itemList.valuationqty
                            },

                            //更新計價/不計
                            alter_valuationqty: function(){
                                if (dialog.itemList.plot_no.indexOf('RW') != -1) {
                                    dialog.itemList.valuationqty = dialog.itemList.issuedefectqty;
                                    dialog.itemList.valuationqty_no = dialog.itemList.returned_qty;
                                }else{
                                    dialog.itemList.valuationqty = (+dialog.itemList.issuedefectqty) + (+dialog.itemList.returned_qty);
                                    dialog.itemList.valuationqty_no = 0;
                                }
                                
                            },

                            //數量檢查
                            Qty_check: function(msg){
                                if((+dialog.itemList.can_returned_qty) < ((+dialog.itemList.returned_qty) + (+dialog.itemList.abnormal_qty) + 
                                    (+dialog.itemList.lossqty) - (+dialog.itemList.excessqty) + (+dialog.itemList.return_qty) + 
                                    (+dialog.itemList.issuedefectqty))) // modify by Alan for#161004 : 不需要卡控外包超收檢查
                                {
                                    msg = $filter('translate')('panelInput.msg.can_return_qty_unenough',{can_returned_qty: dialog.itemList.can_returned_qty});   
                                }else{
                                    if((+dialog.itemList.returned_qty < (+dialog.itemList.excessqty))){
                                        msg = $filter('translate')('panelInput.msg.good_qty_error');   //良品數 的輸入值 需>= 多餘數
                                    }else{
                                        if(((+dialog.itemList.returned_qty)>(+dialog.itemList.can_returned_qty))&& (dialog.osbacknotallowedexceed =="1")){
                                            msg = $filter('translate')('panelInput.msg.OSReceipt_Qtyover');       //外包超收                                           
                                        }else{
                                            if((+dialog.itemList.valuationqty)<(+dialog.itemList.issuedefectqty)){
                                                msg = $filter('translate')('panelInput.msg.valuationqty_error');       //計價數量>=發料不良
                                            }
                                        }
                                    }
                                }
                                return msg;
                            }
                        };
                    }
                );
            };
        }]);
});
